<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Spring Boot部署 Jar包形式 &amp;nbsp;&amp;nbsp;如果在创建Spring Boot项目的时候选择的是jar包形式,则只需要使用maven将项目打成jar包即可. 1mvn pakage &amp;nbsp;&amp;nbsp;Spring Boot项目在打成jar后可以直接运行. War包形式 &amp;nbsp;&amp;nbsp;如果在创建Spring Boot项目的时候选择的是war包形式,则打包的方式与j">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot 部署测试与应用监控">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/05/2016-08-5-Spring-monitor/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="Spring Boot部署 Jar包形式 &amp;nbsp;&amp;nbsp;如果在创建Spring Boot项目的时候选择的是jar包形式,则只需要使用maven将项目打成jar包即可. 1mvn pakage &amp;nbsp;&amp;nbsp;Spring Boot项目在打成jar后可以直接运行. War包形式 &amp;nbsp;&amp;nbsp;如果在创建Spring Boot项目的时候选择的是war包形式,则打包的方式与j">
<meta property="og:locale">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nkz6y4ntj20bm038mxv.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nkz77e95j20fy03w758.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nkz7gwh7j20o104z75l.jpg">
<meta property="article:published_time" content="2016-08-05T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nkz6y4ntj20bm038mxv.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/05/2016-08-5-Spring-monitor/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>Spring Boot 部署测试与应用监控 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Boot%E9%83%A8%E7%BD%B2"><span class="nav-number">1.</span> <span class="nav-text">Spring Boot部署</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Jar%E5%8C%85%E5%BD%A2%E5%BC%8F"><span class="nav-number">1.1.</span> <span class="nav-text">Jar包形式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#War%E5%8C%85%E5%BD%A2%E5%BC%8F"><span class="nav-number">1.2.</span> <span class="nav-text">War包形式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B3%A8%E5%86%8C%E4%B8%BALinux%E6%9C%8D%E5%8A%A1"><span class="nav-number">1.3.</span> <span class="nav-text">注册为Linux服务</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%83%AD%E9%83%A8%E7%BD%B2"><span class="nav-number">2.</span> <span class="nav-text">热部署</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E%E7%83%AD%E9%83%A8%E7%BD%B2"><span class="nav-number">2.1.</span> <span class="nav-text">模板引擎热部署</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E4%BA%8EDocker%E9%83%A8%E7%BD%B2"><span class="nav-number">3.</span> <span class="nav-text">基于Docker部署</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Dockerfile"><span class="nav-number">3.1.</span> <span class="nav-text">Dockerfile</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Example"><span class="nav-number">3.2.</span> <span class="nav-text">Example</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Boot%E9%9B%86%E6%88%90%E6%B5%8B%E8%AF%95"><span class="nav-number">4.</span> <span class="nav-text">Spring Boot集成测试</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Example-1"><span class="nav-number">4.1.</span> <span class="nav-text">Example</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Boot%E5%BA%94%E7%94%A8%E7%9B%91%E6%8E%A7"><span class="nav-number">5.</span> <span class="nav-text">Spring Boot应用监控</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#HTTP"><span class="nav-number">5.1.</span> <span class="nav-text">HTTP</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JMX"><span class="nav-number">6.</span> <span class="nav-text">JMX</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SSH"><span class="nav-number">7.</span> <span class="nav-text">SSH</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%99%BB%E5%BD%95%E7%94%A8%E6%88%B7"><span class="nav-number">7.1.</span> <span class="nav-text">自定义登录用户</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#end"><span class="nav-number">8.</span> <span class="nav-text">end</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/08/05/2016-08-5-Spring-monitor/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring Boot 部署测试与应用监控
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-08-05 18:00:00" itemprop="dateCreated datePublished" datetime="2016-08-05T18:00:00+08:00">2016-08-05</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="Spring-Boot部署"><a href="#Spring-Boot部署" class="headerlink" title="Spring Boot部署"></a>Spring Boot部署</h3><hr>
<h4 id="Jar包形式"><a href="#Jar包形式" class="headerlink" title="Jar包形式"></a>Jar包形式</h4><hr>
<p>&nbsp;&nbsp;如果在创建Spring Boot项目的时候选择的是jar包形式,则只需要使用maven将项目打成jar包即可.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn pakage</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;Spring Boot项目在打成jar后可以直接运行.</p>
<h4 id="War包形式"><a href="#War包形式" class="headerlink" title="War包形式"></a>War包形式</h4><hr>
<p>&nbsp;&nbsp;如果在创建Spring Boot项目的时候选择的是war包形式,则打包的方式与jar一样.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mvn pakage</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;之后将生成的war文件放在任意Servlet容器上运行即可.</p>
<p>&nbsp;&nbsp;如果在创建Spring Boot项目的时候选择的是jar,在部署时又想以war包形式部署,则需要以下配置.</p>
<ol>
<li><p>修改pom.xml文件的打包方式</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nkz6y4ntj20bm038mxv.jpg"></p>
</li>
<li><p>添加以下依赖覆盖默认内嵌的Tomcat.</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6nkz77e95j20fy03w758.jpg"></p>
</li>
<li><p>创建ServletInitializer.</p>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6nkz7gwh7j20o104z75l.jpg"></p>
</li>
</ol>
<h4 id="注册为Linux服务"><a href="#注册为Linux服务" class="headerlink" title="注册为Linux服务"></a>注册为Linux服务</h4><hr>
<p>&nbsp;&nbsp;将软件注册为服务,可以通过命令开启、关闭、开机自启动等功能.</p>
<p>&nbsp;&nbsp;如想注册为Linux服务,则先需要修改spring-boot-maven-plugin的配置:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">   &lt;build&gt;</span><br><span class="line">	&lt;plugins&gt;</span><br><span class="line">		&lt;plugin&gt;</span><br><span class="line">			&lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;spring-boot-maven-plugin&lt;&#x2F;artifactId&gt;</span><br><span class="line">			&lt;configuration&gt;</span><br><span class="line">				&lt;executable&gt;true&lt;&#x2F;executable&gt;</span><br><span class="line">			&lt;&#x2F;configuration&gt;</span><br><span class="line">		&lt;&#x2F;plugin&gt;</span><br><span class="line">	&lt;&#x2F;plugins&gt;</span><br><span class="line">&lt;&#x2F;build&gt;</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;主流的Linux大都使用init.d或systemd来注册服务.</p>
<ul>
<li><p>init.d方式</p>
<ol>
<li><p>sudo ln -s /var/apps/spring-boot-demo.jar /etc/init.d/sdemo,其中sdemo就是服务名.</p>
</li>
<li><p>常用操作命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">service sdemo start 启动服务.</span><br><span class="line">service sdemo stop 停止服务.</span><br><span class="line">service sdemo status 服务状态.</span><br><span class="line">chkconfig sdemo on 开机启动.</span><br></pre></td></tr></table></figure></li>
<li><p>项目日志存放于/var/log/sdemo.log中.</p>
</li>
</ol>
</li>
<li><p>systemd方式</p>
<ol>
<li><p>在/etc/systemd/system/目录下新建文件sdemo.service.并写入以下内容:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description&#x3D;spring-boot-demo</span><br><span class="line">After&#x3D;syslog.target</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">ExecStart&#x3D; &#x2F;usr&#x2F;bin&#x2F;java -jar &#x2F;var&#x2F;apps&#x2F;spring-boot-demo.jar</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy&#x3D;multi-user.target</span><br><span class="line"></span><br><span class="line">#其中Description和ExecStart是可更改的.ExecStart是指定java运行需要运行的jar包.</span><br></pre></td></tr></table></figure>
</li>
<li><p>常用操作命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">systemctl start sdemo 启动服务.</span><br><span class="line">systemctl stop sdemo 停止服务.</span><br><span class="line">systemctl status sdemo 服务状态.</span><br><span class="line">systemctl enable sdemo 开机启动.</span><br><span class="line">journalctl -u sdemo 查看项目日志.</span><br></pre></td></tr></table></figure>

</li>
</ol>
</li>
</ul>
<h3 id="热部署"><a href="#热部署" class="headerlink" title="热部署"></a>热部署</h3><hr>
<p>&nbsp;&nbsp;热部署即就是在应用正在运行的时候升级软件，却不需要重新启动应用.</p>
<p>&nbsp;&nbsp;在Spring Boot项目中添加<strong>spring-boot-devtools</strong>依赖即可实现页面和类的热部署.</p>
<h4 id="模板引擎热部署"><a href="#模板引擎热部署" class="headerlink" title="模板引擎热部署"></a>模板引擎热部署</h4><hr>
<p>&nbsp;&nbsp;Spring Boot默认模板引擎开启缓存,如果我们修改了页面后再刷新页面将得不到修改后的内容,我们可以在application.properties中关闭模板引擎的缓存.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#Thymeleaf</span><br><span class="line">spring.thymeleaf.cache&#x3D;false</span><br><span class="line">#FreeMarker</span><br><span class="line">spring.freemarker.cache&#x3D;false</span><br><span class="line">#Groovy</span><br><span class="line">spring.groovy.template.cache&#x3D;false</span><br><span class="line">#Velocity</span><br><span class="line">spring.velocity.cache&#x3D;false</span><br></pre></td></tr></table></figure>
<h3 id="基于Docker部署"><a href="#基于Docker部署" class="headerlink" title="基于Docker部署"></a>基于Docker部署</h3><hr>
<p>&nbsp;&nbsp;目前主流的PaaS平台都支持发布Docker镜像.而Docker镜像是使用Dockerfile文件来编译镜像的.</p>
<h4 id="Dockerfile"><a href="#Dockerfile" class="headerlink" title="Dockerfile"></a>Dockerfile</h4><hr>
<ol>
<li><p>FROM指令</p>
<p>FROM指令指明了当前镜像继承的基镜像.编译当前镜像时会自动下载基镜像.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FROM java:8</span><br></pre></td></tr></table></figure>
</li>
<li><p>MAINTAINER指令</p>
<p>MAINTAINER指令指明了当前镜像的作者</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MAINTAINER sun</span><br></pre></td></tr></table></figure>
</li>
<li><p>RUN指令</p>
<p>RUN指令可以在当前镜像上执行Linux命令并形成一个新的层.RUN指令是编译时(build)的动作.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">RUN &#x2F;bin&#x2F;bash -c &quot;echo helloworld&quot;</span><br></pre></td></tr></table></figure></li>
<li><p>CMD指令</p>
<p>CMD指令指明镜像启动时的默认行为.一个Dockerfile里只能有一个CMD指令.CMD指令设定的命令可以在运行镜像时使用参数覆盖.它是运行时(run)的动作.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CMD echo &quot;hello&quot;</span><br><span class="line">可被 docker run -d image_name echo &quot;world&quot; 覆盖.</span><br></pre></td></tr></table></figure>
</li>
<li><p>EXPOSE指令</p>
<p>EXPOSE指令指明了镜像运行时的容器必须监听指定的端口.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">EXPOSE 8080</span><br></pre></td></tr></table></figure>
</li>
<li><p>EVN指令</p>
<p>EVN指令用于设置环境变量.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ENV myName&#x3D;sun</span><br></pre></td></tr></table></figure>
</li>
<li><p>ADD指令</p>
<p>ADD指令用于从当前工作目录复制文件到镜像目录.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ADD hello.sh &#x2F;dir&#x2F;</span><br></pre></td></tr></table></figure>
</li>
<li><p>ENTRYPOINT指令</p>
<p>ENTRYPOINT指令可以让容器像一个可执行程序一样运行,这样镜像运行时可以像软件一样接收参数执行,ENTRYPOINT是运行时(run)的动作.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ENTRYPOINT [&quot;&#x2F;bin&#x2F;echo&quot;]</span><br><span class="line">我们可以镜像传递参数:</span><br><span class="line">docker run -d image_name &quot;hello world&quot;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h4 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h4><hr>
<ol>
<li><p>首先将打好包的demo上传到Linux服务器.</p>
</li>
<li><p>在项目同级目录下新建一个Dockerfile文件,如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">FROM java:8</span><br><span class="line"></span><br><span class="line">MAINTAINER&#x3D;sun</span><br><span class="line"></span><br><span class="line">ADD spring-boot-demo.jar app.jar</span><br><span class="line"></span><br><span class="line">EXPOSE 8080</span><br><span class="line"></span><br><span class="line">ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;&#x2F;app.jar&quot;]</span><br></pre></td></tr></table></figure></li>
<li><p>编译镜像</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">在&#x2F;var&#x2F;apps&#x2F;sdemo目录下执行以下命令</span><br><span class="line">docker build -t sun&#x2F;sdemo .</span><br><span class="line">使用sun&#x2F;sdemo作为镜像名称,sun为前缀.  </span><br><span class="line">.是用来指明Dockerfile路径的,这里因为在当前目录下所以使用“.”.</span><br></pre></td></tr></table></figure>
</li>
<li><p>运行镜像</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8080:8080 --name sdemo sun&#x2F;sdemo</span><br></pre></td></tr></table></figure>
<h3 id="Spring-Boot集成测试"><a href="#Spring-Boot集成测试" class="headerlink" title="Spring Boot集成测试"></a>Spring Boot集成测试</h3></li>
</ol>
<hr>
<p>&nbsp;&nbsp;Spring Boot的测试与Spring MVC类似,它提供了一个<code>@SpringApplicationConfiguration</code>替代<code>@ContextConfiguration</code>来指定配置类.</p>
<h4 id="Example-1"><a href="#Example-1" class="headerlink" title="Example"></a>Example</h4><hr>
<ol>
<li><p>Entity</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Entity</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="meta">@GeneratedValue</span></span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Dao</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">PersonRepositroy</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">Person</span>,<span class="title">Long</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Controller</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/person&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    PersonRepositroy personRepositroy;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(method = RequestMethod.GET,produces = &#123;MediaType.APPLICATION_JSON_VALUE&#125;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Person&gt; <span class="title">findAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> personRepositroy.findAll();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Test</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="comment">//@ContextConfiguration(classes = &#123;SpringBootTestApplication.class&#125;)</span></span><br><span class="line"><span class="meta">@SpringApplicationConfiguration(classes = SpringBootTestApplication.class)</span></span><br><span class="line"><span class="meta">@WebAppConfiguration</span></span><br><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonApplicationTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    PersonRepositroy personRepositroy;</span><br><span class="line"></span><br><span class="line">    MockMvc mockMvc;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    WebApplicationContext webApplicationContext;</span><br><span class="line"></span><br><span class="line">    String expectedJson;</span><br><span class="line"></span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">       * 初始化</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">     <span class="meta">@Before</span></span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException </span>&#123;</span><br><span class="line">         Person p1 = <span class="keyword">new</span> Person(<span class="string">&quot;sun&quot;</span>);</span><br><span class="line">         Person p2 = <span class="keyword">new</span> Person(<span class="string">&quot;sylvanas&quot;</span>);</span><br><span class="line">         personRepositroy.save(p1);</span><br><span class="line">         personRepositroy.save(p2);</span><br><span class="line">         <span class="comment">// 获得期待返回的JSON字符串.</span></span><br><span class="line">         expectedJson = Obj2Json(personRepositroy.findAll());</span><br><span class="line">         <span class="comment">// 初始化MockMvc.</span></span><br><span class="line">         mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">     <span class="function"><span class="keyword">protected</span> String <span class="title">Obj2Json</span><span class="params">(Object obj)</span> <span class="keyword">throws</span> JsonProcessingException </span>&#123;</span><br><span class="line">         ObjectMapper objectMapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line">         <span class="keyword">return</span> objectMapper.writeValueAsString(obj);</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">     <span class="meta">@Test</span></span><br><span class="line">     <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testPersonController</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">         String url = <span class="string">&quot;/person&quot;</span>;</span><br><span class="line">         <span class="comment">// 对/person发送请求,获得请求的执行结果.</span></span><br><span class="line">         MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(url)</span><br><span class="line">                 .accept(MediaType.APPLICATION_JSON_VALUE))</span><br><span class="line">                 .andReturn();</span><br><span class="line">         <span class="comment">// 获得执行结果的状态</span></span><br><span class="line">         <span class="keyword">int</span> status = result.getResponse().getStatus();</span><br><span class="line">         <span class="comment">// 获得执行结果的内容</span></span><br><span class="line">         String content = result.getResponse().getContentAsString();</span><br><span class="line">         <span class="comment">// 断言</span></span><br><span class="line">         Assert.assertEquals(<span class="string">&quot;错误:正确的返回值为200&quot;</span>, <span class="number">200</span>, status);</span><br><span class="line">         Assert.assertEquals(<span class="string">&quot;错误:返回值和预期返回值不一致&quot;</span>, expectedJson, content);</span><br><span class="line">     &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Spring-Boot应用监控"><a href="#Spring-Boot应用监控" class="headerlink" title="Spring Boot应用监控"></a>Spring Boot应用监控</h3></li>
</ol>
<hr>
<p>&nbsp;&nbsp;Spring Boot提供了运行时的应用监控和管理的功能.我们可以通过HTTP、JMX、SSH来进行应用的监控.</p>
<h4 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h4><hr>
<p>&nbsp;&nbsp;使用HTTP实现对应用的监控需要添加以下依赖:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-actuator&lt;&#x2F;artifactId&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;<strong>端点:</strong></p>
<table>
<thead>
<tr>
<th>Endpoint</th>
<th>Description</th>
</tr>
</thead>
<tbody><tr>
<td>actuator</td>
<td>所有EndPoint的列表,需要加入spring HATEOAS支持</td>
</tr>
<tr>
<td>autoconfig</td>
<td>当前应用的所有自动配置</td>
</tr>
<tr>
<td>beans</td>
<td>当前应用的所有Bean信息</td>
</tr>
<tr>
<td>configprops</td>
<td>当前应用中所有的配置信息</td>
</tr>
<tr>
<td>dump</td>
<td>显示当前应用线程状态信息</td>
</tr>
<tr>
<td>env</td>
<td>显示当前应用的环境信息</td>
</tr>
<tr>
<td>health</td>
<td>显示当前应用的健康情况</td>
</tr>
<tr>
<td>info</td>
<td>显示当前应用信息</td>
</tr>
<tr>
<td>metrics</td>
<td>显示当前应用的各项指标信息</td>
</tr>
<tr>
<td>mappings</td>
<td>显示所有的@RequestMapping映射的路径</td>
</tr>
<tr>
<td>shutdown</td>
<td>关闭当前应用(默认此项是关闭的)</td>
</tr>
<tr>
<td>trace</td>
<td>默认最新的http请求</td>
</tr>
</tbody></table>
<p>&nbsp;&nbsp;使用http访问应用监控只需要在url中加上/端点名即可.</p>
<ul>
<li>例如: <a target="_blank" rel="noopener" href="http://localhost:8080/actuator">http://localhost:8080/actuator</a></li>
</ul>
<p>&nbsp;&nbsp;端点的开启关闭可以在application.properties中配置.</p>
<p>&nbsp;&nbsp;如果想自定义端点,则需要一个继承AbstractEndpoint的实现类,并注册成Bean即可.</p>
<p>&nbsp;&nbsp;如果想自定义HealthIndicator,则需要一个实现HealthIndicator接口的类,并注册成Bean即可.</p>
<h3 id="JMX"><a href="#JMX" class="headerlink" title="JMX"></a>JMX</h3><hr>
<p>&nbsp;&nbsp;可以使用Java内置的jconsole来实现JMX监控.</p>
<h3 id="SSH"><a href="#SSH" class="headerlink" title="SSH"></a>SSH</h3><hr>
<p>&nbsp;&nbsp;Spring Boot借助CraSH (<a target="_blank" rel="noopener" href="http://www.crashub.org/">http://www.crashub.org</a>), 实现通过SSH或者TELNET监控和管理应用.我们只需要引入依赖spring-boot-starter-remote-shell即可.</p>
<p>&nbsp;&nbsp;Spring Boot在spring-boot-starter-remote-shell的commands包下定制了命令,它使用的是Groovy语言来编写的.Groovy语言是由Spring主导的运行于JVM的动态语言.</p>
<h4 id="自定义登录用户"><a href="#自定义登录用户" class="headerlink" title="自定义登录用户"></a>自定义登录用户</h4><hr>
<p>&nbsp;&nbsp;Spring Boot支持在application.properties中自定义用户的账号密码.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">shell.auth.simple.user.name&#x3D;sun</span><br><span class="line">shell.auth.simple.user.password&#x3D;sun</span><br></pre></td></tr></table></figure>
<h3 id="end"><a href="#end" class="headerlink" title="end"></a>end</h3><blockquote>
<p>资料参考于 JavaEE开发的颠覆者: Spring Boot实战</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/Spring/" rel="tag"># Spring</a>
              <a href="/yuwanzi.io/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/08/04/2016-08-4-Spring-batch/" rel="prev" title="Spring Batch数据批处理">
                  <i class="fa fa-chevron-left"></i> Spring Batch数据批处理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/09/03/2016-09-3-CompilerOpenJDK/" rel="next" title="在CentOS7下手动编译JDK">
                  在CentOS7下手动编译JDK <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
